package com.qingke.note.command;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.qingke.note.App;
import com.qingke.note.pojo.User;

public abstract class Command {
	
	public Command(User user) {
		currentUser = user;
	}
	
	protected User currentUser;
	
	private SqlSession session;
	
	public abstract void execute();
	
	public SqlSession getSession(){
		if(session==null){
			InputStream is = App.class.getClassLoader().getResourceAsStream("resources/mybatis_conf.xml");
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	    	setSession(sessionFactory.openSession());
		}
		return session;
	}
	
	private void setSession(SqlSession session){
		this.session = session;
	}
}
